$(function () {
    $('body').on('click', 'button[data-minus]', function () {
        updateBuyNumber(-1);
    }).on('click', 'button[data-plus]', function () {
        updateBuyNumber(1);
    }).on('click', 'h4[data-tab]', function () {
        $(this).addClass('active').siblings().removeClass('active');
        var dataTab = $(this).data('tab');
        $('section[data-tab=' + dataTab + ']').removeClass('hide').siblings().addClass('hide');
    }).on('keyup', 'input[data-stock]', function () {
        setBuyNumber(parseInt(this.value));
    }).on('click', '[data-buy]', function () {
        var buyNumber = $('input[data-stock]').val();
        var stockNum = $('#stockNum').val();
        if (buyNumber<1) {
            layer.msg("当前库存【"+stockNum+"本】，库存不足")
            return;
        }
        var bookId = $('#bookId').val();
        window.location = baseloaction + '/web/uc/order/initPayOrder?orderType=COMMON&shopData=' + bookId + '-BOOK-' + buyNumber;
    }).on('click', '[data-course]', function () {
        var courseId = $(this).data('course');
        window.location = baseloaction + '/web/course/info?courseId=' + courseId;
    }).on('click',"#book-online-consultation",function () {
        var content = $(this).data('book');
        window.open(content,'newwindow', 'toolbar=no,scrollbars=yes,location=no,resizable=no,top=200,left=300,width=800,height=600');
    });

    /**
     * 设置购买数量
     * @param number
     */
    function setBuyNumber(number) {
        var stock = $('small[data-stock]').data('stock');
        stock = parseInt(stock);
        if (number > stock)
            number = stock;
        if (number != number || number < 1)
            number = 1;
        $('input[data-stock]').val(number);
    }

    /**
     * 更新购买数量
     * @param step 单词加减数量
     */
    function updateBuyNumber(step) {
        var buyNumber = $('input[data-stock]').val();
        buyNumber = parseInt(buyNumber);
        buyNumber += step;
        setBuyNumber(buyNumber);
    }
});